package com.mw.wmimobile.gui.fragments;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.mw.wmimobile.datastorage.DataStorage;
import com.mw.wmimobile.datastorage.StaffDto;
import com.mw.wmimobile.gui.activities.StaffPersonActivity;
import com.wm.wmimobile.R;

public class StaffFragment extends FragmentActivity {

	private EditText findText;
	private ImageView generalButton;
	private ImageView favButton;
	private ListView listView;
	private List<StaffDto> staffDtoList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.staff_activity_list);

		listView = (ListView) findViewById(R.id.StaffActivityListView);

		// tutaj cos smiesznego, zaleznego od przycisku

		loadData(true);
		


		generalButton = (ImageView) findViewById(R.id.GeneralButton);
		generalButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				loadData(true);
			}
		});

		favButton = (ImageView) findViewById(R.id.FavButton);
		favButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				loadData(false);
			}
		});
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					final int position, long id) {

				Intent intent = new Intent(StaffFragment.this,
						StaffPersonActivity.class);
				String codeIntent = staffDtoList.get(position).getId();
				intent.putExtra("codeIntent", codeIntent);
				startActivity(intent);

			}
		});
		
		findText = (EditText) findViewById(R.id.StaffFindText);
		
		findText.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String text = s.toString().toLowerCase(Locale.getDefault());
				find(text);
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
				String text = s.toString().toLowerCase(Locale.getDefault());
				find(text);
			}
		});

	}

	private void loadData(boolean all) {
		if (all) {
				staffDtoList = DataStorage.getInstance().getStaffDtoList();
				showData();
		}
		
		//else PREFFERED
		else {
				//it is already set from database, or from "add to favourite"
				staffDtoList = DataStorage.getInstance().getFavStaffDtoList();
				showData();
		}
	}
	
	public void find(String text) {
		List<StaffDto> tmpStaffList = new ArrayList<StaffDto>();
		if (text != null && text.length() > 0) {
			Integer i = 0;
			for (StaffDto staffDto : DataStorage.getInstance()
					.getStaffDtoList()) {
				if (staffDto.getName().toLowerCase(Locale.getDefault())
						.contains(text)) {
					tmpStaffList.add(staffDto);
					i++;
				}
			}
			staffDtoList = tmpStaffList;
		} else {
			staffDtoList = DataStorage.getInstance().getStaffDtoList();
		}

		showData();
		listView.invalidate();
		//findEditText.requestFocus();
	}
	
	private void showData() {
		listView.setAdapter(new StaffAdapter(StaffFragment.this, staffDtoList));
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		listView.setAdapter(new StaffAdapter(StaffFragment.this,
				staffDtoList));
		listView.invalidate();
	}

}
